package cn.iocoder.yudao.module.bk.controller.admin.lwbookoutapply;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.bk.controller.admin.lwbook.vo.LwBookRespVO;
import cn.iocoder.yudao.module.bk.controller.admin.lwbookinapply.vo.BkImportExcelVO;
import cn.iocoder.yudao.module.bk.controller.admin.lwbookinapply.vo.BkImportRespVO;
import cn.iocoder.yudao.module.bk.controller.admin.lwbookinapply.vo.InExamineReq;
import cn.iocoder.yudao.module.bk.controller.admin.lwbookoutapply.vo.LwBookOutApplyPageReqVO;
import cn.iocoder.yudao.module.bk.controller.admin.lwbookoutapply.vo.LwBookOutApplyRespVO;
import cn.iocoder.yudao.module.bk.controller.admin.lwbookoutapply.vo.LwBookOutApplySaveReqVO;
import cn.iocoder.yudao.module.bk.controller.app.vo.IdEntReq;
import cn.iocoder.yudao.module.bk.dal.dataobject.lwbookoutapply.LwBookOutApplyDO;
import cn.iocoder.yudao.module.bk.service.lwbookoutapply.LwBookOutApplyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 书籍飘出")
@RestController
@RequestMapping("/bk/lw-book-out-apply")
@Validated
public class LwBookOutApplyController {

    @Resource
    private LwBookOutApplyService lwBookOutApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建书籍飘出")
    @PreAuthorize("@ss.hasPermission('bk:lw-book-out-apply:create')")
    public CommonResult<Long> createLwBookOutApply(@Valid @RequestBody LwBookOutApplySaveReqVO createReqVO) {
        return success(lwBookOutApplyService.createLwBookOutApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新书籍飘出")
    @PreAuthorize("@ss.hasPermission('bk:lw-book-out-apply:update')")
    public CommonResult<Boolean> updateLwBookOutApply(@Valid @RequestBody LwBookOutApplySaveReqVO updateReqVO) {
        lwBookOutApplyService.updateLwBookOutApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除书籍飘出")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bk:lw-book-out-apply:delete')")
    public CommonResult<Boolean> deleteLwBookOutApply(@RequestParam("id") Long id) {
        lwBookOutApplyService.deleteLwBookOutApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得书籍飘出")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bk:lw-book-out-apply:query')")
    public CommonResult<LwBookOutApplyRespVO> getLwBookOutApply(@RequestParam("id") Long id) {
        LwBookOutApplyDO lwBookOutApply = lwBookOutApplyService.getLwBookOutApply(id);
        return success(BeanUtils.toBean(lwBookOutApply, LwBookOutApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得书籍飘出分页")
    @PreAuthorize("@ss.hasPermission('bk:lw-book-out-apply:query')")
    public CommonResult<PageResult<LwBookOutApplyRespVO>> getLwBookOutApplyPage(@Valid LwBookOutApplyPageReqVO pageReqVO) {
        PageResult<LwBookOutApplyDO> pageResult = lwBookOutApplyService.getLwBookOutApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LwBookOutApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出书籍飘出 Excel")
    @PreAuthorize("@ss.hasPermission('bk:lw-book-out-apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLwBookOutApplyExcel(@Valid LwBookOutApplyPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LwBookOutApplyDO> list = lwBookOutApplyService.getLwBookOutApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "书籍飘出.xls", "数据", LwBookOutApplyRespVO.class,
                BeanUtils.toBean(list, LwBookOutApplyRespVO.class));
    }

    @PostMapping("/outApplyExamine")
    @Operation(summary = "飘出审核")
    public CommonResult<?> outApplyExamine(@Valid @RequestBody InExamineReq req) {
        return lwBookOutApplyService.outApplyExamine(req);
    }

    @PostMapping("/applyBooks")
    @Operation(summary = "飘出申请书籍")
    public CommonResult<List<LwBookRespVO>> applyBooks(@Valid @RequestBody IdEntReq req) {
        return lwBookOutApplyService.applyBooks(req);
    }

    @PostMapping("/import")
    @Operation(summary = "导入记录")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('system:user:import')")
    public CommonResult<BkImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                    @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<BkImportExcelVO> list = ExcelUtils.read(file, BkImportExcelVO.class);
        return success(lwBookOutApplyService.importBkList(list, updateSupport));
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获取导入漂出模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<BkImportExcelVO> list = Arrays.asList(
                BkImportExcelVO.builder().username("yunai").idCard("510xxxxxxxx")
                        .submitTime(LocalDateTime.now()).bookName("xxxx")
                        .isbn("9873334123").status("漂出").build()
        );
        // 输出
        ExcelUtils.write(response, "书籍漂出模板.xls", "漂出列表", BkImportExcelVO.class, list);
    }



}
